<template>
  <div class="com-container">
    <div class="title" :style="comStyle">
      <span>{{ '┃ '+showTitle}}</span>
      <span class="iconfont title-icon" :style="comStyle" @click="showChoice = !showChoice">&#xe6eb;</span>
      <div class="select-com" :style="marginStyle" v-show="showChoice">
        <div
          class="select-item"
          :style="selectStyle"
          v-for="item in selectTypes"
          :key="item.key"
          @click="handleSelect(item.key)"
        >{{item.text}}</div>
      </div>
    </div>
    <div class="com-chart" ref="chart_ref"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getThemeValue } from '../utils/theme_utils'
export default {
  data() {
    return {
      chartInstance: null, // echart实例
      allData: null, // 服务器返回的数据
      showChoice: false, // 是否显示可选项
      choiceType: 'map', // 显示的数据类型
      titleFontSize: 0 // 指明标题字体大小
    }
  },
  computed: {
    selectTypes() {
      if (!this.allData) {
        return []
      } else {
        return this.allData.type.filter(item => {
          return item.key !== this.choiceType
        })
      }
    },
    showTitle() {
      if (!this.allData) {
        return ''
      } else {
        return this.allData[this.choiceType].title
      }
    },
    // 设置给标题的样式
    comStyle() {
      return {
        fontSize: this.titleFontSize + 'px',
        color: getThemeValue(this.theme).titleColor
      }
    },
    marginStyle() {
      return {
        marginLeft: this.titleFontSize + 'px'
      }
    },
    selectStyle() {
      return {
        backgroundColor: getThemeValue(this.theme).selectBackground
      }
    },
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      // console.log('主题切换了')
      this.chartInstance.dispose() // 销毁当前图表
      this.initChart() // 从新以最新主题初始化图表对象
      this.screenAdapter() // 完成屏幕适配
      this.updateChart() // 更新图表的展示
    }
  },
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    // 在页面加载完成的时候，主动进行屏幕的适配
    this.screenAdapter()
  },
  destroyed() {
    // 在组件销毁的时候需要把监听器取消掉，防止内存的泄露
    window.removeEventListener('resize', this.screenAdapter)
  },
  methods: {
    // 初始化echartInstance对象
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.chart_ref, this.theme)
      // 对图表初始化配置的控制
      const initOption = {
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          left: 20,
          top: '15%',
          icon: 'circle'
        },
        grid: {
          top: '35%',
          left: '3%',
          right: '4%',
          bottom: '1%',
          containLabel: true // 距离是包含坐标轴文字
        },
        xAxis: {
          type: 'category',
          boundaryGap: false // 是否需要具备间隔间隙
        },
        yAxis: {
          type: 'value'
        }
      }
      this.chartInstance.setOption(initOption)
    },
    // 获取服务器数据
    getData() {
      const data = {
        type: [{ key: 'map', text: '地区销量趋势' }, { key: 'seller', text: '商家销量趋势' }, { key: 'goods', text: '商品销量趋势' }],
        common: {
          month: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
        },
        map: {
          title: '地区销量趋势',
          base: 310,
          unit: '万',
          data: [
            {
              name: '广州',
              data: [1132, 1223, 1234, 569, 789, 1456, 759, 985, 1111, 1234, 1263, 1234]
            },
            {
              name: '北京',
              data: [2252, 2523, 2234, 2569, 2789, 2056, 2759, 2985, 2111, 2234, 2263, 2234]
            },
            {
              name: '深圳',
              data: [3132, 3223, 3234, 3569, 3789, 3456, 3759, 3985, 3111, 3234, 3263, 3234]
            },
            {
              name: '上海',
              data: [4132, 4223, 4234, 4569, 4789, 4456, 4459, 4985, 4111, 4234, 4263, 4234]
            },
            {
              name: '重庆',
              data: [5732, 5223, 5234, 5569, 5789, 5456, 5759, 5985, 5141, 5834, 5163, 5734]
            }
          ]
        },
        seller: {
          title: '商家销量趋势',
          base: 310,
          unit: '万',
          data: [
            {
              name: '商家1',
              data: [113, 122, 123, 569, 789, 145, 759, 985, 111, 134, 263, 234]
            },
            {
              name: '商家2',
              data: [225, 253, 224, 269, 279, 206, 279, 285, 211, 224, 226, 223]
            },
            {
              name: '商家3',
              data: [312, 323, 324, 356, 378, 346, 379, 395, 311, 324, 326, 323]
            },
            {
              name: '商家4',
              data: [413, 423, 423, 459, 478, 445, 445, 498, 411, 423, 426, 423]
            },
            {
              name: '商家5',
              data: [573, 522, 524, 556, 578, 545, 575, 595, 514, 583, 516, 573]
            }
          ]
        },
        goods: {
          title: '商品销量趋势',
          base: 310,
          unit: '万',
          data: [
            {
              name: '手机数码',
              data: [1134, 1272, 1237, 5769, 7589, 145, 7459, 9585, 1141, 1344, 2463, 2434]
            },
            {
              name: '女装',
              data: [2252, 2534, 2724, 2469, 2479, 2046, 2719, 2854, 2141, 2224, 2246, 2243]
            },
            {
              name: '大家电',
              data: [3412, 3323, 3244, 3356, 7378, 3446, 7379, 6395, 3411, 3244, 3246, 7323]
            },
            {
              name: '男装',
              data: [413, 423, 423, 459, 478, 445, 445, 498, 411, 423, 426, 423]
            },
            {
              name: '美妆护肤',
              data: [573, 522, 524, 556, 578, 545, 575, 595, 514, 583, 516, 573]
            }
          ]
        }
      }

      this.allData = data
      this.updateChart()
    },
    // 更新图表
    updateChart() {
      // 半透明的颜色值
      const colorArr1 = [
        'rgba(252,151,175,0.5)',
        'rgba(135,247,207,0.5)',
        'rgba(247, 244, 148,0.5)',
        'rgba(125, 224, 255,0.5)',
        'rgba(247, 197, 160,0.5)'
      ]
      // 全透明的颜色值
      const colorArr2 = [
        'rgba(252,151,175,0)',
        'rgba(135,247,207,0)',
        'rgba(247, 244, 148,0)',
        'rgba(125, 224, 255,0)',
        'rgba(247, 197, 160,0)'
      ]
      // x轴类目轴的数据
      const timeArr = this.allData.common.month
      // y轴的数据 series的数据
      const valueArr = this.allData[this.choiceType].data
      const seriesArr = valueArr.map((item, index) => {
        return {
          name: item.name,
          type: 'line',
          data: item.data,
          stack: this.choiceType,
          areaStyle: {
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              // 0%状态下的颜色值
              {
                offset: 0,
                color: colorArr1[index]
              },
              // 100%状态下的颜色值
              {
                offset: 1,
                color: colorArr2[index]
              }
            ])
          }
        }
      })
      // 图例的数据
      const legendArr = valueArr.map(item => {
        return item.name
      })
      const dataoption = {
        xAxis: {
          data: timeArr
        },
        legend: {
          data: legendArr
        },
        series: seriesArr
      }
      this.chartInstance.setOption(dataoption)
    },
    // 当浏览器大小发生变化的时候，会调用该方法来适配屏幕
    screenAdapter() {
      this.titleFontSize = this.$refs.chart_ref.offsetWidth / 100 * 3.6 // 这个比例比较合适
      // 和分辨率大小相关的配置项
      const adapterOption = {
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize,
          textStyle: {
            fontSize: this.titleFontSize / 2
          }
        }
      }
      this.chartInstance.setOption(adapterOption)
      // 需要手动调用图表对象的resize方法才能产生效果
      this.chartInstance.resize()
    },
    // 处理选择切换图表数据
    handleSelect(currentType) {
      this.choiceType = currentType
      this.showChoice = false
      this.updateChart()
    }
  }
}
</script>

<style lang="less" scoped>
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 10;
  color: white;
  cursor: pointer;
  .title-icon {
    margin-left: 10px;
    cursor: pointer;
  }
  .select-com {
    background-color: #293441;
  }
}
</style>
